package com.share.dgs.fetcher;

import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsData;
import graphql.schema.DataFetchingEnvironment;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author caifan
 * @created 2021/3/5
 * description:
 */
@DgsComponent
public class FileUploadMutation {
    @DgsData(parentType = "Mutation", field = "uploadFile")
    public boolean uploadFile(DataFetchingEnvironment dataFetchingEnvironment) {
        Map<String,Object> input = dataFetchingEnvironment.getArgument("input");
        @SuppressWarnings("unchecked")
        List<MultipartFile> parts = (List<MultipartFile>) input.get("files");
        parts.forEach(it -> {
            String content = null;
            try {
                content = new String(it.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        return !parts.isEmpty();
    }
}

@Data
class FileUploadInput {
    private String description;
    private List<MultipartFile> files;

}
